/**
 * Copyright (c) 2016-2019 人人开源 All rights reserved.
 *
 * https://www.renren.io
 *
 * 版权所有，侵权必究！
 */

package io.renren.modules.app.controller;
import java.math.BigDecimal;


import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.conditions.update.UpdateChainWrapper;
import io.renren.common.utils.*;
import io.renren.modules.app.annotation.Delay;
import io.renren.modules.app.annotation.Login;
import io.renren.modules.app.entity.BalanceLogEntity;
import io.renren.modules.app.entity.BalanceOrderEntity;
import io.renren.modules.app.entity.CustomerEntity;
import io.renren.modules.app.service.BalanceLogService;
import io.renren.modules.app.service.BalanceOrderService;
import io.renren.modules.app.service.CustomerService;
import io.renren.modules.app.service.PublicService;
import io.renren.modules.app.utils.AuthUtils;
import io.renren.modules.sys.redis.SysConfigRedis;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.util.Date;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * APP测试接口
 *
 * @author Mark sunlightcs@gmail.com
 */
@RestController
@RequestMapping("/api")
@Api(tags = "APP测试接口")
public class AppTestController {

    @Resource
    public BalanceOrderService balanceOrderService;

    @Resource
    private CustomerService customerService;

    @Resource
    private BalanceLogService balanceLogService;

    @Resource
    private RedisUtils redisUtils;

    @Resource
    protected HttpServletRequest httpServletRequest;

    @Resource
    protected PublicService publicService;

    @Resource
    protected SysConfigRedis sysConfigRedis;

    @Autowired
    private EnvironmentHelper environmentHelper;

    private Lock lock = new ReentrantLock();



    @Resource
    RabbitTemplate rabbitTemplate;


    @RequestMapping("dead_letter")
    public String sendMsg(){
//        rabbitTemplate.convertAndSend("order-event-exchange","order.create.order","这是一条私信队列的测试信息");
        rabbitTemplate.convertAndSend("order-event-exchange","order.create.order","这是一条私信队列的测试信息");
        return "发送成功";
    }

    @Login
    @PostMapping("userInfo")
    @ApiOperation("获取用户信息")
    public R userInfo(@RequestBody String data){
        return R.ok().put("user", data);
    }

    @Login
    @GetMapping("getUserInfo")
    @ApiOperation("获取用户信息")
    public R getUserInfo(){
        Integer userId = AuthUtils.getUserId();
        return R.ok().put("userId", userId);
    }

    @Login
    @GetMapping("userId")
//    @ApiOperation(value = "获取用户ID", notes = "传入payOrder", response = PayOrderRecordAccountPageVO.class)
    @ApiOperation("获取用户ID")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "当前页码，从1开始", paramType = "query", required = true, dataType="int") ,
            @ApiImplicitParam(name = "limit", value = "每页显示记录数", paramType = "query",required = true, dataType="int") ,
            @ApiImplicitParam(name = "order_field", value = "排序字段", paramType = "query", dataType="String") ,
            @ApiImplicitParam(name = "order", value = "排序方式，可选值(asc、desc)", paramType = "query", dataType="String") ,
            @ApiImplicitParam(name = "username", value = "用户名", paramType = "query", dataType="String")
    })
    public R userInfo(@RequestAttribute("userId") Integer userId){
        return R.ok().put("userId", userId);
    }

    @Delay(time = 5000)
    @GetMapping("notToken")
    @ApiOperation("忽略Token验证测试")
    public R notToken(){
        return R.ok().put("msg", "无需token也能访问。。。");
    }

    @GetMapping("index")
    @ApiOperation("忽略Token验证测试")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "goodsId", value = "商品id", paramType = "query", required = true),
            @ApiImplicitParam(name = "id", value = "用户ID", paramType = "query", required = true, dataType = "Integer")
    })
//    @Transactional(propagation = Propagation.REQUIRED,rollbackFor = Exception.class)
    public R index(@RequestParam("goodsId") Long goodsId,@RequestParam("id") Integer id){
//        CustomerEntity customerEntity = new CustomerEntity();
//        customerEntity.setAuthSalt("2");
//        Boolean ret = customerService.update(customerEntity,new UpdateWrapper<CustomerEntity>().eq("user_id",1).set("username","mark2"));
//        return R.ok().put("data", sysConfigRedis.get("is_reg")).put("ret",ret);
        System.out.println();
//        return customerService.testCrud();
        return R.ok();
    }

}
